Page({
    data: {
      orderId: '',
      orderId2: '',
      orderNo: '',
      name: '',
      userTel: '',
      province: '',
      city: '',
      county: '',
      detailAddress: '',

      bookInfos: [],


      date: '',
      bookPrice: 0,
      scoreUsed: 0,
      cutDwonFee: 0,
      needToPay: 0,

      shipPrice: 0,
      price: '',

      localname: '',

      status: '',

      discountFee: 0,


      lefttime: 0,

      surPrice: 0,


      timeContent: '',

      bookIdstr: '',
    },


    getOrder (order) {

      var _this = this
      var params = {
        orderId: order
      }
      wx.request({
        url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/order/purchase/detail/auth',
        data: params,
        method: 'POST',

        header: {
          "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
          "token": wx.getStorageSync('token'),
          "uid": wx.getStorageSync('uuid')
        },
        success: (res) => {
          if (res.data.code == 200) {
            _this.setData({
              orderNo: res.data.data.orderNo,
              date: res.data.data.date.replace(/-/g, '/'),
              allDiscountFee: res.data.data.allDiscountFee,
              bookPrice: res.data.data.formerPrice,
              discountFee: res.data.data.discountFee,

              shipPrice: res.data.data.shipPrice,

              surPrice: res.data.data.surPrice,
              // needToPay: res.data.data.preOrderInfos.needToPay,
              cutDwonFee: res.data.data.cutDownFee,
              // price: res.data.data.price,
              bookInfos: res.data.data.bookInfo,
              name: res.data.data.name,
              userTel: res.data.data.userTel,
              localname:
                res.data.data.province +
                res.data.data.city +
                res.data.data.county +
                res.data.data.detailAddress,
              status: res.data.data.status
            // city: ,
            })
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        }
      })
    },

    onLoad (options) {
     // console.log(JSON.parse(options.bookArr))
      this.setData({
        orderId: options.orderno,
        bookArr: JSON.parse(decodeURIComponent(options.bookArr)),
        orderId2: options.orderId,
        orderIds: options.orderIds,
        bookIdstr: options.bookIdstr,
        lefttime: (900 - options.lefttime)
      })

      this.getOrder(this.data.orderId)

      this.cutTime()
    },

  cutTime() {


    var _this = this

    // var time = _this.data.lefttime



    var time


    time = setInterval(function () {


      if (_this.data.lefttime == 0) {
        clearInterval(time)

        _this.setData({
          status: 4
        })

        return
      }
      _this.setData({
        lefttime: _this.data.lefttime - 1
      })



    }, 1000)






  },


    //取消订单
  cutOrders () {
    var _this = this
    wx.showModal({
      title: '取消订单',
      content: '是否取消订单',
      success: (res) => {
        if (res.cancel) {
          return false
        }else{
          _this.cutOrderApi()
        }
      }
    })
  },

  cutOrderApi () {
    var params = {
      orderId: this.data.orderId
    }
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/order/purchase/cancelBy/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          wx.showToast({
            title: '订单取消成功',
            icon: 'none',
            duration: 2000
          })
          setTimeout(function () {
            wx.navigateBack()
          },2000)
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },


  //去支付

  paySuccess () {
    wx.navigateTo({
      url: '/package/index/pages/buyBook/buyBook?bookArr=' + encodeURIComponent(JSON.stringify(this.data.bookArr)) + '&orderId=' + this.data.orderId2 + '&bookIdstr=' + this.data.bookIdstr + '&orderIds=' + this.data.orderIds
    })
  },

  paySuccess2 () {
    return false
  }
})
